function url(c,a,param){
    if(!param) param = "";
    return "http://localhost/guimi.us/index.php?c="+c+"&a="+a+param;
};
function kset(key,value) 
{ 
    window.localStorage.setItem( key , value ); 
}
function kget( key ) 
{ 
    return window.localStorage.getItem( key ); 
}
function kremove( key ) 
{ 
    window.localStorage.removeItem( key ); 
}

$(function(){
//首页
    if(kget("username")){
        $("#btnLogin").html(kget("username"));
         $("#btnBack").css({"display":"block"});        
    }else{
        $("#btnLogin").html("登录");
        $("#btnBack").css({"display":"none"});
        $("#btnLogin").click(function(){
            openNew();
        });         
    }
    $("#btnBack").click(function(){
        kremove("username");
        $.get( url("main","logout") );
        location.href = "http://localhost/guimi.us/index.php";
    });  
    // 其他隐藏
    $("#j-user").css({"display":"none"});
    $("#create_article").css({"display":"none"});
//首页加载
    $.post(url("article","article_list"),function(data){
        if(data[0]["article_id"]){
            // var str = "";
            var list_str ="";
            for(var i=0;i<data.length;i++){
                var str = '<div class="card yxj">'
                    +      '<div class="tab"></div>'
                    +      '<div class="article" id="'+data[i].article_id+'">'
                    +           '<h2>'+data[i].article_title+'</h2>'
                    +           '<div class="article_info">'
                    +               data[i].add_time+'  发表人：'+data[i].user_name
                    +           '</div>'
                    +            '<p><img class="attachment-medium wp-card-image" title="#" alt="#" src="tpl/img/ali_big.jpg"></p>'
                    +           '<p>'+data[i].article_content
                    +            '<a class="more" id="more'+i+'">更多</a>'
                    +           '</p>'
                    +           '<div class="chat">'
                    +               '<div class="chat_news" id="chat_news'+i+'">'
                    +               '</div>' 
                    +               '<div class="inputbox l">'  
                    +                  '<input type="text" class="j-comment" placeholder="随便说几句吧O(∩_∩)O~">'       
                    +               '</div>'
                    +                '<div class="btn_cont r">'
                    +                        '<span class="arrow_solid"></span>'
                    +                        '<a id="btn_ok'+i+'">ok</a>'
                    +                '</div>'
                    +           '</div>'
                    +       '</div>'
                    +'</div>'                
                    +'</div>'; 
                
                $("#j-article_list").append(str);
                $("#more"+i).click(function(){
                    var that = $(this);
                    var article_id = that.parents(".article").attr("id");
                    if(that.text()=="更多"){
                        that.text("收起");
                        $.post(url("comment","comment_list"),{article_id:article_id},function(data){
                            if(data != null){
                                var h_str ="";
                                for(var i=0;i<data.length;i++){
                                    if(i%2==0){
                                         h_str += '<div id="yxj" class="chat_news_cont">'
                                                   +     '<div class="user_picture l"><img src="tpl/img/ali_little.jpg"></div>'
                                                   +     '<div class="user_news_info_l l">'
                                                   +        '<span class="chat_arrow_solid chat_arrow_solid_l"></span>'
                                                    +        '<h6 class="l">'+data[i].user_name+'</h6>'
                                                    +        '<div style="clear:both;">'
                                                    +            '<div class="l">'+data[i].comment_content+'</div>'
                                                    +        '</div>'
                                                    +    '</div>'
                                                    + '</div>';                                
                                    }else{
                                         h_str += '<div id="ww" class="chat_news_cont">'
                                                    +    '<div class="user_picture r"><img src="tpl/img/ali_little.jpg"></div>'
                                                    +    '<div class="user_news_info_r r">'
                                                    +        '<span class="chat_arrow_solid chat_arrow_solid_r"></span>'
                                                    +        '<h6 class="r">'+data[i].user_name+'</h6>'
                                                    +        '<div style="clear:both;"><div class="r">'+data[i].comment_content+'</div></div>'
                                                    +    '</div>'
                                                    + '</div>';
                                    }
                                }
                                that.parent().siblings(".chat").find(".chat_news").css({"display":"block"}).empty().append(h_str);
                            }else{
                                that.text("更多");
                               alert("该文章没有评论"); 
                            }

                        },"json");                        
                    }else{
                        that.text("更多");
                        that.parent().siblings(".chat").find(".chat_news").css({"display":"none"})
                    }

                });
                $("#btn_ok"+i).click(function(){
                    var that = $(this);
                    var content = $(".j-comment").val();
                    var article_id = $(".article").attr("id");
                    $.post(url("comment","add_comment"),{content:content,article_id:article_id,user_id:kget("id"),user_name:kget("username")},function(data){
                        if(data.length !=0){
                            if(data[0].id%2 !=0){
                                var h_str = '<div id="ww" class="chat_news_cont">'
                                            +    '<div class="user_picture r"><img src="tpl/img/ali_little.jpg"></div>'
                                            +    '<div class="user_news_info_r r">'
                                            +        '<span class="chat_arrow_solid chat_arrow_solid_r"></span>'
                                            +        '<h6 class="r">'+data[0].user_name+'</h6>'
                                            +        '<div style="clear:both;"><div class="r">'+data[0].content+'</div></div>'
                                            +    '</div>'
                                            + '</div>';                                
                            }else{
                                var h_str = '<div id="yxj" class="chat_news_cont">'
                                           +     '<div class="user_picture l"><img src="tpl/img/ali_little.jpg"></div>'
                                           +     '<div class="user_news_info_l l">'
                                           +        '<span class="chat_arrow_solid chat_arrow_solid_l"></span>'
                                            +        '<h6 class="l">'+data[0].user_name+'</h6>'
                                            +        '<div style="clear:both;">'
                                            +            '<div class="l">'+data[0].content+'</div>'
                                            +        '</div>'
                                            +    '</div>'
                                            + '</div>';                                  
                            }
                            that.parent().siblings(".chat_news").css({"display":"block"}).append(h_str);
                        }else{
                            alert(data.errmsg);
                        }
                    },"json");                    
                });        
            };
            for(var j=0;j<5;j++){
                list_str += '<li>'
                        +       '<img src="tpl/img/ali_big.jpg">'
                        +       '<a href="#"><h6>'+data[j].article_title+'<h6>'
                        +       '<div class="cardinfo">发表人：'+data[j].user_name+' </br>'+data[j].add_time+'</div>'
                        +       '</a>'
                        +    '</li>';                  
            };
            // $("#j-article_list").append(str);
            $("#j-list").append(list_str);
        }else{
          alert(data.errmsg);
        }   
    },"json");  
// 登录弹出框
     function openNew(){
        //插入遮罩层的div
        var oMask=$("<div id='mask'>");
        oMask.css({
            width:"100%",
            height:"100%"
        });
        oMask.appendTo("body");

        //插入login的div
        var oLogin=$("<div id='login'>"); 
        oLogin_html="<div class='loginCon'><div class='login_header'><span>登陆</span><button id='closeBtnLogin'></button></div><div class='login_content'><div class='inputbox'><input type='text' name='user' id='username' placeholder='用户名''></div><div class='inputbox'><input type='password' name='password' id='pwd' placeholder='密码''></div><div class='operate'><label class='l'><input type='checkbox' checked='checked' id='auto-signin'>自动登录</label><a href='#' class='r' >忘记密码?</a></div><div class='inputbox'><input type='button' value='登录'id='signin-btn'></div></div>"
        oLogin.html(oLogin_html);
        oLogin.appendTo("body");
        //获取login的宽高
        var dHeight=oLogin.height();
        var dWidth=oLogin.width();
        oLogin.css({
            top:"50%",
            left:"50%",
            margin:-dHeight/2+"px 0px 0px "+(-dWidth/2)+"px"
        });

        $("#closeBtnLogin").click(function(){
            oMask.remove();
            oLogin.remove();
        });
        $("#signin-btn").click(function(){
            var username = $("#username").val();
            var pwd = $("#pwd").val();
            $.post(url("main","login"),{username:username,pwd:pwd},function(data){
                if(typeof(data.id) != "undefined"){
                  kset("id",data.id);
                  kset("username",data.username);
                  $("#btnLogin").html(kget("username"));
                  location.href = "http://localhost/guimi.us/index.php";
                }else{
                  alert(data.errmsg);
                }   
            },"json");
        });
        
    } 
//发布页面
    $("#j-create_btn").click(function(){
        $("#j-article_list").css({"display":"none"});
        $("#j-user").css({"display":"none"});
        $("#create_article").css({"display":"block"});
    });
    $("#j-publish").click(function(){
        var title = $("#j-title").val();
        var content = $("#j-content").val();
        $.post(url("article","add_article"),{title:title,content:content},function(data){
            if(data == 1){
                $("#j-article_list").css({"display":"block"});
                $("#j-user").css({"display":"none"});
                $("#create_article").css({"display":"none"});
                location.href = "http://localhost/guimi.us/index.php";
            }else{
              alert(data.errmsg);
            }   
        },"json");        
    }); 
    $("#header .yxj").click(function(){
                 
    });
});

$(function(){
        // 焦点图轮播
        var container = $('#ppt');
        var list = $('#photo');
        var buttons = $('#buttons li');
        var prev = $('#prev');
        var next = $('#next');
        var index = 1;
        var len = 5;
        var animated = false;
        var interval = 3000;
        var timer;
         function animate (offset) {
            var left = parseInt(list.css('left')) + offset;
            if (offset>0) {
                offset = '+=' + offset;
            }
            else {
                offset = '-=' + Math.abs(offset);
            }
            list.animate({'left': offset}, 300, function () {
                if(left > -200){
                    list.css('left', -300 * len);
                }
                if(left < (-300 * len)) {
                    list.css('left', -300);
                }
            });
        }

        function showButton() {
            buttons.eq(index-1).addClass('on').siblings().removeClass('on');
        }

        function play() {
            timer = setTimeout(function () {
                next.trigger('click');
                play();
            }, interval);
        }
        function stop() {
            clearTimeout(timer);
        }

        next.bind('click', function () {
            if (list.is(':animated')) {
                return;
            }
            if (index == 5) {
                index = 1;
            }
            else {
                index += 1;
            }
            animate(-300);
            showButton();
        });

        prev.bind('click', function () {
            if (list.is(':animated')) {
                return;
            }
            if (index == 1) {
                index = 5;
            }
            else {
                index -= 1;
            }
            animate(300);
            showButton();
        });

        buttons.each(function () {
             $(this).bind('click', function () {
                 if (list.is(':animated') || $(this).attr('class')=='on') {
                     return;
                 }
                 var myIndex = parseInt($(this).attr('index'));
                 var offset = -300 * (myIndex - index);

                 animate(offset);
                 index = myIndex;
                 showButton();
             })
        });

        container.hover(stop, play);

        play();    
});